/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rultax.matrix;

import com.rultax.neural.feedforward.FeedForwardLayer;
import com.rultax.neural.feedforward.FeedForwardNetwork;

/**
 *
 * @author Scott
 */
public class MatrixCODEC {

    public static void arrayToNetwork(final Double array[], final FeedForwardNetwork network){
        int index = 0;
        
        for(final FeedForwardLayer layer: network.getLayers()){
            
            if(layer.getNext() != null)
                index = layer.getMatrix().fromPackedArray(array, index);
        }
    }
    
    public static Double[] networkToArray(final FeedForwardNetwork network){
        int size = 0;
        
        for(final FeedForwardLayer layer : network.getLayers())
            if(layer.hasMatrix())
                size += layer.getMatrixSize();
        
        final Double result[] = new Double[size];
        
        int index = 0;
        
        for (final FeedForwardLayer layer : network.getLayers()) {
            if(layer.getNext() != null){
                final Double matrix[] = layer.getMatrix().toPackedArray();
                for (int i = 0; i < matrix.length; i++) {
                    result[index++] = matrix[i];
                }
            }
        }
        
        return result;
    }
}
